package com.gsy.springai.controller;


import com.gsy.springai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;



@Slf4j
@RestController
@RequestMapping("/ai")
//使用lombok的无参构造方法
@RequiredArgsConstructor
public class ChatController {
    //能否使用autow进行自动注入？
    private  final ChatClient chatClient;

    private final ChatHistoryRepository chatHistoryRepository;

    @RequestMapping(value = "/chat" , produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt ,String chatId){

        //1.保存会话id
        chatHistoryRepository.save("chat", chatId);

        //2、请求模型
        return chatClient.prompt()
                .user(prompt)
                //使用的是一起输出
                //.call()
                //传入相同的会话id会接着之前额提问进行回答
                .advisors(a ->a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream()
                .content();
    }









}
